package cn.uniqueinfo.dp.controller;

import cn.uniqueinfo.dp.controller.base.BaseController;
import cn.uniqueinfo.dp.exception.UQException;
import cn.uniqueinfo.dp.service.IFormGridColumnService;
import cn.uniqueinfo.dp.service.IFormGridService;
import cn.uniqueinfo.dp.service.IFormService;
import cn.uniqueinfo.dp.util.ResultUtil;
import cn.uniqueinfo.dp.vo.*;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-23 10:54:35
 * @description:业务表单基础配置
 */
@Slf4j
@Controller
@RequestMapping(value = "/form")
public class FormController extends BaseController<FormVO> {

    @Autowired private IFormGridColumnService formGridColumnService;
    @Autowired private IFormGridService formGridService;
    private IFormService service;

    public FormController(IFormService service) {
        this.service = service;
        this.baseService = service;
        this.ctlName = "form";
        this.tipName = "业务表单配置";
    }

    /** 业务表单表格内容配置页面 */
    @GetMapping(value = "/grid")
    public ModelAndView grid() {
        this.checkPermission("grid", "grid");
        return new ModelAndView("/" + ctlName + "/grid");
    }

    /** 查询数据表配置的业务表单 */
    @GetMapping("/info")
    @ResponseBody
    public List<FormVO> info(@RequestParam(value = "tn") String tableName) {
        return this.service.findFormsByTableName(tableName);
    }

    /**
     * @author:朱元勋
     * @date:2019/1/24 09:12
     * @description: 解析sql获取字段元数据
     * @param: sql 要解析的sql字符串
     * @param：formName 业务标识
     * @param: sqlParam sql字符串中的参数
     * @return:java.util.List<cn.uniqueinfo.dp.vo.FormGridColumnVO>
     */
    @PostMapping("/jx")
    @ResponseBody
    public List<FormGridColumnVO> getColumnsFromSql(
            @RequestParam(value = "s") String sql,
            @RequestParam(value = "f") String formName,
            @RequestParam(value = "p") String sqlParam)
            throws UQException {
        return this.formGridColumnService.getColumnsFromSql(formName, sql, sqlParam);
    }

    /**
     * @author:朱元勋
     * @date:2019/1/24 10:14
     * @description:获取业务表单表格配置信息
     * @param:formName 业务表单标识
     * @param:tableName 物理表名称
     * @return:cn.uniqueinfo.dp.vo.FormGridVO
     */
    @PostMapping("/fg")
    @ResponseBody
    public FormGridVO findFormGrid(
            @RequestParam(value = "fn") String formName,
            @RequestParam(value = "tn") String tableName) {
        return this.formGridService.findByFormName(formName);
    }

    @PostMapping("/fgc")
    @ResponseBody
    public List<FormGridColumnVO> findGridColumns(@RequestParam(value = "fn") String formName) {
        return this.formGridColumnService.findGridColumnsByFormName(formName);
    }

    @PostMapping("/sc")
    @ResponseBody
    public Result saveConfig(@RequestParam(name = "p", required = true) String p) {
        SaveItem item = JSONObject.parseObject(p, SaveItem.class);
        log.info("size:" + item.getRows().size());
        FormGridVO formGridVO = item.getFg();
        if ("add".equalsIgnoreCase(item.getType())) {
            this.formGridService.insert(formGridVO);
        } else {
            this.formGridService.update(formGridVO);
            // 批量删除之前的配置
            this.formGridColumnService.deleteByFormName(formGridVO.getFormName());
        }

        log.info("grid id :" + formGridVO.getId());
        // 批量插入配置
        List<FormGridColumnVO> fgcItems = item.getRows();
        for (FormGridColumnVO fgcItem : fgcItems) {
            fgcItem.setGridId(formGridVO.getId());
            fgcItem.setFormName(formGridVO.getFormName());
            this.formGridColumnService.insert(fgcItem);
        }

        return ResultUtil.success();
    }
}
